package cn.iocoder.yudao.module.dine.config.service.admin;

import cn.iocoder.yudao.framework.business.basic.pojo.dto.SimpleQueryDTO;
import cn.iocoder.yudao.framework.business.basic.service.PageService;
import cn.iocoder.yudao.framework.business.basic.service.impl.BaseService;
import cn.iocoder.yudao.framework.datapermission.core.annotation.DataPermission;
import cn.iocoder.yudao.module.api.dine.vo.DineConfigVO;
import cn.iocoder.yudao.module.dine.config.convert.DineConfigConvert;
import cn.iocoder.yudao.module.dine.config.dao.DineConfigMapper;
import cn.iocoder.yudao.module.dine.config.pojo.dto.DineConfigSaveDTO;
import cn.iocoder.yudao.module.dine.config.pojo.po.DineConfig;
import cn.iocoder.yudao.module.dine.config.service.core.CoreDineConfigService;
import jakarta.annotation.Resource;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * <pre>
 * OOoO0OOoO0OOOooo0oOOOO0OOOOO0oooOO0ooOOO0Ooooo0OOOOo0ooooO0OOooo0Ooooo0OOOOO
 * 服务
 * OOoO0OOoO0OOOooo0oOOOO0OOOOO0oooOO0ooOOO0Ooooo0OOOOo0ooooO0OOooo0Ooooo0OOOOO
 * </pre>
 *
 * @author 山野羡民（1032694760@qq.com）
 * @since 2025/08/01
 */
@Service
public class DineConfigService extends BaseService<DineConfig, DineConfigVO, SimpleQueryDTO> implements PageService<DineConfigVO, SimpleQueryDTO> {
    @Resource
    private DineConfigMapper dineConfigMapper;

    @Resource
    @Lazy // 延迟加载，避免循环依赖
    private CoreDineConfigService coreDineConfigService;

    @Override
    public DineConfigMapper getInjectedMapper() {
        return dineConfigMapper;
    }

    @Override
    public DineConfigVO convertToViewObject(DineConfig entity) {
        return DineConfigConvert.INSTANCE.toViewObject(entity);
    }

    @DataPermission(enable = false)
    @Transactional(rollbackFor = Exception.class)
    public Boolean save(Long shopId, DineConfigSaveDTO param) {
        DineConfig entity = DineConfigConvert.INSTANCE.toEntity(param);
        entity.setShopId(shopId);
        DineConfig config = coreDineConfigService.getOrCreate(shopId);
        entity.setId(config.getId());
        return dineConfigMapper.updateById(entity) > 0;
    }

}
